//
//  ChangeSexVC.m
//  Cunli
//
//  Created by lilubin on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ChangeSexVC.h"
#import "MyDataService.h"
#import "Config.h"
#import "NSString+MD5.h"
#import "User.h"

#define kUIScreenWidth [UIScreen mainScreen].bounds.size.width
#define kUIScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ChangeSexVC () <UIPickerViewDelegate, UIPickerViewDataSource>
{
        NSArray *_sexTypes;
}

@property (weak, nonatomic) IBOutlet UITextField *sexSelField;

@property (nonatomic, strong) NSString *userSex;

@property (nonatomic, strong) UIPickerView *sexTypePickerView;

@end

@implementation ChangeSexVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([CLUser shareCLUser].truename != nil) {
    if([Login curLoginDetailUser].sex != nil){
//        _sexSelField.text = [CLUser shareCLUser].sex;
        _sexSelField.text = [Login curLoginDetailUser].sex;
    }
    _userSex = @"1";
    [self initAllGlobalParams];
    [self setAllUIKitAttributes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initAllGlobalParams
{
    _sexTypes = [NSArray arrayWithObjects:@"男", @"女", nil];
}

- (void)setAllUIKitAttributes
{
    
    self.title = @"性别";
    
    _sexTypePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, kUIScreenHeight - 200, kUIScreenWidth-40, 280)];
    _sexTypePickerView.delegate = self;
    _sexTypePickerView.dataSource = self;
    _sexTypePickerView.backgroundColor = [UIColor clearColor];
    
    _sexSelField.inputView = _sexTypePickerView;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"regnext"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    
    if (_userSex.length != 0) {
        NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
        [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
        [CheckParams setObject:_userSex forKey:@"value"];
        [CheckParams setObject:@"sex" forKey:@"field"];
        
        NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
        [CheckParams setObject:resultSign forKey:@"sign"];
        
        [MyDataService requestURL:@"edit_profile.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
            if ([result[@"response"] isEqualToString:@"edit_profile"])
            {
//                [CLUser shareCLUser].sex = _sexSelField.text;
                [Login curLoginDetailUser].sex = _sexSelField.text;
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                [Config showHUDWithText:@"姓名修改失败" andImage:@"error"];
            }
        } failureAction:^{
            [Config showHUDWithText:@"姓名修改失败" andImage:@"error"];
        }];
    }

}

#pragma mark -  UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 2;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _userSex = [NSString stringWithFormat:@"%ld", row+1];
    _sexSelField.text = _sexTypes[row];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    _sexSelField.text = _sexTypes[row];
    _userSex = [NSString stringWithFormat:@"%ld", row+1];
    return _sexTypes[row];
}



@end
